import Vue from 'vue'
import VueRouter from 'vue-router'
import {getToken} from '@/utils/auth'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    meta: {title: '群众呼声智慧大屏'},
    component: () => import('../views/index.vue')
}, {
    path: '/login',
    name: 'login',
    meta: {title: '登录 - 群众呼声智慧大屏'},
    component: () => import('../views/login.vue')
}]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    const isLoggedIn = getToken()||true;
    if (!isLoggedIn && to.path != '/login') {
        // 如果未登录且试图访问仪表盘，则重定向到登录页
      router.push('/login');
    } else {
        // 否则允许导航
        next();
    }
});
export default router
